package com.bankcomm.ui.adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.bankcomm.R;
import com.bankcomm.bean.AroundShopListObj;
import com.bankcomm.net.http.IXAsyncImageLoader;

/**
 * 身边优惠信息Adapter
 * 
 * @author zhang.h
 * 
 * @date 2012-6-14 下午01:42:30
 */
public class AroundShopListAdapter extends MainBaseAdapter<AroundShopListObj> {

	public AroundShopListAdapter(Activity context, ListView listView) {
		super(context);
		setListView(listView);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		View itemView = convertView;
		if (itemView == null) {
			itemView = mContext.getLayoutInflater().inflate(R.layout.special_shop_list_item, null);

			holder = new ViewHolder();
			holder.aroundShopIV = (ImageView) itemView.findViewById(R.id.shop_item_image);
			holder.aroundShopName = (TextView) itemView.findViewById(R.id.shop_item_name);
			holder.aroundShopGradeRB = (RatingBar) itemView.findViewById(R.id.rateing_bar);
			holder.aroundShopGrade = (TextView) itemView.findViewById(R.id.rateing_bar_text);
			holder.aroundShopAddress = (TextView) itemView.findViewById(R.id.shop_item_address);
            holder.hotPopInfo=(ImageView)itemView.findViewById(R.id.im_TH_Logo);
			holder.aroundShopDistanceLayout = (LinearLayout) itemView.findViewById(R.id.shop_item_gps_layout);
			holder.aroundShopDistance = (TextView) itemView.findViewById(R.id.shop_item_distance);
			holder.aroundShopTelNo=(TextView) itemView.findViewById(R.id.shop_tel_no);
			

			itemView.setTag(holder);
		} else {
			holder = (ViewHolder) itemView.getTag();
		}

		// 图片
		// load the image and set it on the ImageView BEGIN
		String imageUrl = mList.get(position).getAroundShopImageUrl();
		ImageView imageView = holder.aroundShopIV;
		imageView.setTag(imageUrl);
		imageView.setScaleType(ScaleType.FIT_XY);

		Drawable drawable = new IXAsyncImageLoader().loadDrawable(imageUrl, new IXAsyncImageLoader.ImageCallback() {
			public void imageLoaded(Drawable imageDrawable, String imageUrl) {
				ImageView imageViewByTag = (ImageView) mListView.findViewWithTag(imageUrl);
				if (imageViewByTag != null) {
					if (imageDrawable != null) {
						imageViewByTag.setImageDrawable(imageDrawable);
					}
				}
			}
		});

		if (drawable == null) {
			// 默认图片
			imageView.setImageResource(R.drawable.img_waiting);
		} else {
			imageView.setImageDrawable(drawable);
		}
		// load the image and set it on the ImageView END

		// 名称
		holder.aroundShopName.setText(mList.get(position).getAroundShopName());

		// 评分
		int grade = mList.get(position).getAroundShopGrade();
		float gradeF = (float) ((grade / 10f) / 2f);
		holder.aroundShopGradeRB.setRating(gradeF);
		holder.aroundShopGrade.setText((float) grade / 10 + "分");

		// 地址
		holder.aroundShopAddress.setText(mList.get(position).getAroundShopAddress());

		// 优惠类型
		String markType = mList.get(position).getAroundShopMarkType();
		String[] markTypes = markType.split(",");

//
//		// 多个优惠类型
		if (markTypes.length > 1) {
			for (int i = 0; i < markTypes.length; i++) {
				String currMarkType = markTypes[i];
				addMarkTypeView(holder.hotPopInfo, currMarkType);
			}
		}
		// 单个优惠类型
		else {
			addMarkTypeView(holder.hotPopInfo, markType);
		}

		// 距离
		String distance = mList.get(position).getAroundShopDistance();
		if (distance != null && distance.length() > 0) {
			holder.aroundShopDistanceLayout.setVisibility(View.VISIBLE);
			holder.aroundShopDistance.setText(distance + "m");
		} else {
			holder.aroundShopDistanceLayout.setVisibility(View.GONE);
		}
		
         holder.aroundShopTelNo.setText(mList.get(position).getAroundShopTelNo());
		return itemView;
	}

	/**<item>特别优惠</item>
	 *<item>返刷卡金</item>
	 *<item>其他活动</item>
	 *<item>SPECIAL</item>
		*<item>REDFRIDAY</item>
		*<item>MARKETACTIVE</item>
	 **/
	private void addMarkTypeView(ImageView currIV, String currMarkType) {

		// 特惠
		if (currMarkType != null && "SPECIAL".equals(currMarkType)) {
			currIV.setImageResource(R.drawable.shop_hui);
		}
		//其他活动
		else if (currMarkType != null && "MARKETACTIVE".equals(currMarkType)) {
			currIV.setImageResource(R.drawable.shop_more);
		}
		// 最红星期五
		else if (currMarkType != null && "REDFRIDAY".equals(currMarkType)) {
			currIV.setImageResource(R.drawable.shop_fan);
		}
//		// 分期宝
//		else if (currMarkType != null && "FQB".equals(currMarkType)) {
//
//		}
//		// 营销活动
//		else if (currMarkType != null && "MARKETACTIVE".equals(currMarkType)) {
//
//		}
//		// 卧龙港
//		else if (currMarkType != null && "WLG".equals(currMarkType)) {
//
//		}
	}

	static class ViewHolder {
		ImageView aroundShopIV;
		TextView aroundShopName;
		RatingBar aroundShopGradeRB;
		TextView aroundShopGrade;
		TextView aroundShopAddress;
        ImageView hotPopInfo;
		LinearLayout aroundShopDistanceLayout;
		TextView aroundShopDistance;
		TextView aroundShopTelNo;
	}

}
